package com.yc.cloud.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.cloud.entity.HwMonitoringPoint;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 监视点管理表 Mapper 接口
 * </p>
 *
 * @author Claude Code
 * @since 2025-09-29
 */
@Mapper
public interface HwMonitoringPointMapper extends BaseMapper<HwMonitoringPoint> {

    /**
     * 根据区域ID查询监视点列表
     *
     * @param regionId 区域ID
     * @return 监视点列表
     */
    default List<HwMonitoringPoint> selectByRegionId(Long regionId) {
        return this.selectList(
            new QueryWrapper<HwMonitoringPoint>()
                .eq(HwMonitoringPoint.RegionId, regionId)
                .eq(HwMonitoringPoint.IsDeleted, 0)
                .orderByAsc(HwMonitoringPoint.PointType, HwMonitoringPoint.ImportanceLevel, HwMonitoringPoint.Name)
        );
    }

    /**
     * 根据监视点类型查询
     *
     * @param pointType 监视点类型
     * @return 监视点列表
     */
    default List<HwMonitoringPoint> selectByPointType(Integer pointType) {
        return this.selectList(
            new QueryWrapper<HwMonitoringPoint>()
                .eq(HwMonitoringPoint.PointType, pointType)
                .eq(HwMonitoringPoint.IsDeleted, 0)
                .orderByAsc(HwMonitoringPoint.ImportanceLevel, HwMonitoringPoint.Name)
        );
    }

    /**
     * 根据重要程度查询
     *
     * @param importanceLevel 重要程度
     * @return 监视点列表
     */
    default List<HwMonitoringPoint> selectByImportanceLevel(Integer importanceLevel) {
        return this.selectList(
            new QueryWrapper<HwMonitoringPoint>()
                .eq(HwMonitoringPoint.ImportanceLevel, importanceLevel)
                .eq(HwMonitoringPoint.IsDeleted, 0)
                .orderByAsc(HwMonitoringPoint.RegionId, HwMonitoringPoint.Name)
        );
    }

    /**
     * 根据状态查询
     *
     * @param status 状态
     * @return 监视点列表
     */
    default List<HwMonitoringPoint> selectByStatus(Integer status) {
        return this.selectList(
            new QueryWrapper<HwMonitoringPoint>()
                .eq(HwMonitoringPoint.Status, status)
                .eq(HwMonitoringPoint.IsDeleted, 0)
                .orderByAsc(HwMonitoringPoint.RegionId, HwMonitoringPoint.ImportanceLevel)
        );
    }

    /**
     * 根据监视点编码查询
     *
     * @param code 监视点编码
     * @return 监视点对象
     */
    default HwMonitoringPoint selectByCode(String code) {
        return this.selectOne(
            new QueryWrapper<HwMonitoringPoint>()
                .eq(HwMonitoringPoint.Code, code)
                .eq(HwMonitoringPoint.IsDeleted, 0)
        );
    }

    /**
     * 根据区域ID列表查询监视点
     *
     * @param regionIds 区域ID列表
     * @return 监视点列表
     */
    default List<HwMonitoringPoint> selectByRegionIds(List<Long> regionIds) {
        if (regionIds == null || regionIds.isEmpty()) {
            return List.of();
        }
        return this.selectList(
            new QueryWrapper<HwMonitoringPoint>()
                .in(HwMonitoringPoint.RegionId, regionIds)
                .eq(HwMonitoringPoint.IsDeleted, 0)
                .orderByAsc(HwMonitoringPoint.RegionId, HwMonitoringPoint.ImportanceLevel, HwMonitoringPoint.Name)
        );
    }

    /**
     * 检查监视点编码是否已存在
     *
     * @param code 监视点编码
     * @param excludeId 排除的ID（用于更新时检查）
     * @return 是否存在
     */
    default boolean existsByCode(String code, Long excludeId) {
        QueryWrapper<HwMonitoringPoint> query = new QueryWrapper<HwMonitoringPoint>()
            .eq(HwMonitoringPoint.Code, code)
            .eq(HwMonitoringPoint.IsDeleted, 0);
        
        if (excludeId != null) {
            query.ne(HwMonitoringPoint.ID, excludeId);
        }
        
        return this.selectCount(query) > 0;
    }

    /**
     * 获取指定区域下的监视点ID列表
     *
     * @param regionId 区域ID
     * @return 监视点ID列表
     */
    default List<Long> selectIdsByRegionId(Long regionId) {
        return this.selectList(
            new QueryWrapper<HwMonitoringPoint>()
                .select(HwMonitoringPoint.ID)
                .eq(HwMonitoringPoint.RegionId, regionId)
                .eq(HwMonitoringPoint.IsDeleted, 0)
        ).stream().map(HwMonitoringPoint::getId).collect(Collectors.toList());
    }

    /**
     * 统计区域下的监视点数量
     *
     * @param regionId 区域ID
     * @return 监视点数量
     */
    default Long countByRegionId(Long regionId) {
        return this.selectCount(
            new QueryWrapper<HwMonitoringPoint>()
                .eq(HwMonitoringPoint.RegionId, regionId)
                .eq(HwMonitoringPoint.IsDeleted, 0)
        );
    }
}